import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/bezier_bounce_footer.dart';
import 'package:flutter_easyrefresh/bezier_hour_glass_header.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:village_android/model/menu_model.dart';
import 'package:village_android/model/monitor_report_model.dart';
import 'package:village_android/model/point_model.dart';
import 'package:village_android/pages/monitor/monitor_report_manager_page.dart';
import 'package:village_android/utils/common_utils.dart';
import 'package:village_android/utils/http_utlis.dart';

import '../browser.dart';
import 'monitor_appeal_browser.dart';
import 'monitor_appeal_record_list.dart';

class MonitorReportListPage extends StatefulWidget {
  MenuModel menuModel;

  MonitorReportListPage({Key key, this.menuModel}) : super(key: key);

  @override
  _MonitorReportListPageState createState() => _MonitorReportListPageState();
}
//NewsCell

class _MonitorReportListPageState extends State<MonitorReportListPage> {
  List items = [];

  @override
  void initState() {
    super.initState();
    getData();
  }

  void getData() {
    var userId = CommonUtils.getUserId();
    if(widget.menuModel.type.toString() == "2") {
      userId = "";
    }

    HttpUtils.get("/api/healthReport/list", data: {
      "type": widget.menuModel.type.toString(),
      "targetUserId": userId,
      "userId":""
    }, success: (data) {
      setState(() {
        items = data;
        print(items);
      });
    }, error: (errorMsg) {
      print(errorMsg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.menuModel.title),
          actions: widget.menuModel.type == 0 ? <Widget>[
            new RaisedButton(
                color: Colors.red,
                child: Text(
                  "申诉记录",
                  style: TextStyle(color: Colors.white),
                ),
                onPressed: () {
                  var page = new MonitorAppealRecordPage(
                  );
                  Navigator.push(context,
                      new MaterialPageRoute(builder: (context) => page));
                }),
          ] : <Widget>[]
        ),
        body: Center(
          child: EasyRefresh(
            header: BezierHourGlassHeader(backgroundColor: Colors.red),
            footer: BezierBounceFooter(backgroundColor: Colors.red),
            onRefresh: () async {
              getData();
            },
            onLoad: () async {},
            child: ListView.separated(
                itemBuilder: (context, index) {
                  MonitorReportModel model =
                  MonitorReportModel.fromJson(items[index]);
                  return _newsCell(context, model);
                },
                separatorBuilder: (context, index) =>
                    Divider(height: 1.0, color: Colors.black12),
                itemCount: items.length),
          ),
        ),
      ),
    );
  }

  void onItemClick(MonitorReportModel item) {

    if(item.type.toString() == '0') {
      var page = new MonitorAppealBrowserPage(
        title: item.title,
        url: item.url,
        healthReportId: item.healthReportId,
      );
      Navigator.push(context, new MaterialPageRoute(builder: (context) => page));
    }else{
      var page = new BrowserPage(
        title: item.title,
        url: item.url,
      );
      Navigator.push(context, new MaterialPageRoute(builder: (context) => page));
    }

  }

  //独立Item
  Widget _newsCell(BuildContext context, MonitorReportModel item) {
    Widget coverWidget(item) {
      return FadeInImage.assetNetwork(
        placeholder: 'assets/empty_image.png',
        image: item.coverUrl,
        width: ScreenUtil().setWidth(100),
        height: ScreenUtil().setHeight(100),
        fit: BoxFit.fitWidth,
      );
    }

    return new GestureDetector(
      onTap: () {
        onItemClick(item);
      },
      child: Container(
        height: ScreenUtil().setHeight(170),
        alignment: Alignment.center,
        padding: EdgeInsets.all(10.0),
        margin: EdgeInsets.all(10.0),
        color: Colors.white30,
        child: Row(
          children: <Widget>[
            coverWidget(item),
            Padding(padding: EdgeInsets.only(left: 15)),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Text(
                    item.title,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                  Text(
                    "内容:　" + item.content,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                  Text(
                    "监督人:　" + item.reportUserName,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      fontSize: 15,
                      color: Colors.black,
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(top: 2)),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
